using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;

using ExchangeHog.Pop3Client.Responses;

namespace ExchangeHog.Pop3Client.Commands
{
	internal sealed class StatCommand : Pop3Command<StatResponse>
	{
		public StatCommand(Stream stream) : base(stream, false, Pop3State.Transaction) { }

		protected override byte[] CreateRequestMessage() { return GetRequestMessage(Pop3Commands.Stat); }

		protected override StatResponse CreateResponse(byte[] buffer)
		{
			Pop3Response response = Pop3Response.CreateResponse(buffer);
			string[] values = response.HostMessage.Split(' ');

			//
			//	should consist of '+OK', 'messagecount', 'octets'
			//
			if (values.Length < 3)
				throw new Pop3Exception(string.Concat("Invalid response message: ", response.HostMessage));

			int messageCount = Convert.ToInt32(values[1]);
			long octets = Convert.ToInt64(values[2]);

			return new StatResponse(response, messageCount, octets);
		}
	}
}
